/*
文件说明：商品规格块信息处理
修改时间：2010-06-14
*/
(function(){
	spec_SellProperty=function(I,B,A,C,D){this.init(I,B,A,C,D);}
	spec_SellProperty.prototype={
		init:function(I,B,A,C,D){
			this.editaccessoriesno=(D==undefined?false:D);
			this.sellProps=B;
			this.allItems=A;
			this.callback=C;
			this.idPrefix=I;
			this.selectPVID={};
			this.selectCPID=[];
		},
		initload:function(){
			for(var I in this.sellProps){
				var way=this.sellProps[I].way;
				$(this.idPrefix+"spec_value_"+I).attr("way",way);
				if(way==2){
					$(this.idPrefix+"spec_value_"+I).parent().bind("click",function(){
						var s_id=$(this).find("span:first").attr("id");
						var dd=$(this).parent().find("dd");
						var showno=$(this).attr("showno");
						if(showno=="1"){
							$(this).attr("showno",0);
							$(this).removeClass("dtselected");
							dd.removeClass("ddselected").hide();
						}else{
							$(".items-info dl>dt[showno='1']>span[id!='"+s_id+"']").click();
							$(this).attr("showno",1);
							$(this).addClass("dtselected");
							dd.addClass("ddselected").show();
						}
					}).attr("showno",0);
					$(this.idPrefix+"spec_value_"+I).parent().parent().addClass("handle_"+way);
				}
				if(way==3){
					var tmp_select=$("<select></select>");
					tmp_select.append("<option value='' cpid=0 pvid=0>"+lodo_lang_js.goods_spec.way3select+"</option>");
/*					var tmp_ul=$(this.idPrefix+"spec_value_"+I).parent().parent().find("dd>ul.ul-color-pic>li>a>span");
					tmp_ul.each(function(){
						var s_name=$(this).attr("title");
						var cpid=$(this).parent().attr("cpid");
						var pvid=$(this).parent().attr("pvid");
						var tmp_option=$("<option value='"+s_name+"' cpid='"+cpid+"' pvid='"+pvid+"'>"+s_name+"</option>");
						tmp_select.append(tmp_option);
					});*/
					var idp=this.idPrefix;
					tmp_select.change(function(){
						var option=$(this).find("option:selected");
						var s_name=option.val();
						var cpid=option.attr("cpid");
						var pvid=option.attr("pvid");
						var selectli;
						if(cpid!="0"&&pvid!="0"){
							selectli=$(idp+pvid+"[class!='selected']");
						}else{
							selectli=$(this).parent().parent().parent().find("dd>ul.ul-color-pic>li.selected");
						}
						if(selectli){selectli.find("a").click();}
					});
					$(this.idPrefix+"spec_value_"+I).html(tmp_select);
					$(this.idPrefix+"spec_value_"+I).parent().parent().addClass("handle_"+way);
				}
			}
			
			var K_C={};
			var D=0;
			for(var E in this.defaultSelect){D++}
			if(D>0){
				for(cpid in this.defaultSelect){
					this.selectHandle(cpid,this.defaultSelect[cpid],false)
				}
			}else{
				K_C=this.onlyOneSelect();
			}
			this.updateDOM();
			this.doCallback(K_C);
		},
		setDefaultItemId:function(A){this.defaultItemId=A||0},
		setDefaultSelect:function(A){this.defaultSelect=A||{}},
		setSellPropsCount:function(A){this.sellPropsCount=A||0},
		observe:function(D){
			var A=D?D.getAttribute("cpid"):0;
			var B=D?D.getAttribute("pvid"):"";
			var C=this.selectHandle(A,B,true);
			C.show=false;
			if(C){this.doCallback(C)}
			return false
		},
		onlyOneSelect:function(){
			var K={};
			for(var C in this.sellProps){
				var A=C;
				var B=this.sellProps[C].pvs;
				var E=0;
				var D;
				for(var F in B){
					if(!B[F].instock){continue}
					E++;
					D=F
				}
				if(E==1){K=this.selectHandle(A,D,false)}
			}
			K.show=true;
			return K;
		},
		selectHandle:function(A,D,C){
			var G=null;
			var H=this.sellProps[A]||{};
			var B=H.pvs||{};
			if(B[D]&&!B[D].instock){return null}
			for(var E in B){
				if(E==D){
					G=E
				}else{
					if(B[E].selected){
						B[E].selected=!B[E].selected;
						this.pvSelect(B[E])
					}
				}
			}
			if(!G){return null}
			B[G].selected=!B[G].selected;
			this.pvSelect(B[G]);
			if(C){this.updateDOM()}
			F=B[G];
			return F
		},
		doCallback:function(A){
			var B={sellPropsCount:this.sellPropsCount,currentPV:this.getCurrentPV()};
			$(B).attr(A);
			if(this.callback){this.callback(B)}
		},
		pvSelect:function(F){
			var B=F.cpid;
			var D=F.pvid;
			var C=F.selected;
			var E=$(this.idPrefix+(D||""));
			var A=$(this.idPrefix+(D||"")+"_i");
			if(C){
				$(E).addClass("selected");
				$(A).show();
				this.selectPVID[B]=D;
				if(this.selectCPID.indexOf(B)==-1){this.selectCPID.push(B)}
			}else{
				$(E).removeClass("selected");
				$(A).hide();
				delete this.selectPVID[B];
				this.selectCPID.remove(B)
			}
		},
		updateDOM:function(){
			for(var A in this.sellProps){
				this.unstockInGray((this.sellProps[A]||{}),A)
			}
			this.updatePriceStock()
		},
		unstockInGray:function(C,J){
			var B={};
			$(B).attr(this.selectPVID);
			var D=C.pvs||{};
			for(var I in D){
				if(I){
					B[J]=I;
					var G=false;
					for(var H=0;!G&&H<this.allItems.length;H++){
						var L=this.allItems[H].itemid;
						var A=this.allItems[H].itemDetils;
						for(var F=0;!G&&F<A.length;F++){
							var N=A[F]||{};
							//if(parseInt(N.stock)<=0&&N.stock!=-1){continue}
							var K=true;
							
							for(var E in B){
								if(!G&&(N.prop||"").indexOf(B[E])==-1){K=false;break;}
							}
							G=K
						}
					}
				var M=D[I];
				M.instock=G;
				this.inGray(M,G)
				}
			}
		},
		inGray:function(C,A){
			var D=$(this.idPrefix+(C.pvid||""));
			var B=D.children().eq(0);
			if(D[0]&&B[0]){
				var K=D.parent().parent().parent().find("dt>span>select");
				if(A){
					$(D).css("cursor","").removeClass("outofstock");
					if(K.find("option[pvid='"+C.pvid+"']").size()<=0){
						var s_name=D.find("a>span").html()
						K.append("<option value='"+s_name+"' cpid='"+$(B).attr("cpid")+"' pvid='"+C.pvid+"'>"+s_name+"</option>");
					}
				}else{
					$(D).css("cursor","default").addClass("outofstock");
					if(K[0]){K.find("option[pvid='"+C.pvid+"']").remove();}
				}
			}
		},
		updatePriceStock:function(){
			var J=this.getCurrentPV();
			for(var S=0;S<this.allItems.length;S++){
				var Q="0.00";
				var T="0.00";
				var D="";
				var P=0;
				var C=0;
				var F=0;
				var Jname="";
				var selected=false;
				var L=this.allItems[S].itemid;
				var B=this.allItems[S].itemDetils;
				for(var R=0;R<B.length;R++){
					var A=B[R];
					if(A.prop==J){
						Q=A.price;
						T=A.save;
						D=A.discount;
						P=A.point;
						C=A.stock;
						F=A.detailId;
						Jname=A.name;
						selected=true;
						break
					}
				}
				$(this.idPrefix+"buyqn").val($(this.idPrefix+"buyqn").attr("lv"));
				if(L==this.defaultItemId){
					var H=$(this.idPrefix+"buy_itemDetailId");
					if(H[0]){
						if(F!=0&&(C>=-1)){
							H.val(F);
						}else{
							H.val("");
						}
					}
					if(this.selectCPID.length!=this.sellPropsCount){C=this.allItems[S].quantity};
					var G=$(this.idPrefix+"stock");
					$(this.idPrefix+"addToCart,"+this.idPrefix+"addmake,"+this.idPrefix+"nowbuyToCart,"+this.idPrefix+"goods_pointsexchange_info").show();
					$(this.idPrefix+"noAddToCart").hide();
					//if(G[0]){
						if(C!=-1){
							if(C<=0){
								G.find("em").html(lodo_lang_js.goods_spec.noqn);
								G.find("span").hide();
								$(this.idPrefix+"addToCart,"+this.idPrefix+"addmake,"+this.idPrefix+"nowbuyToCart,"+this.idPrefix+"goods_pointsexchange_info").hide();
								$(this.idPrefix+"noAddToCart").show();
							}else{
								if(G.find("em")[0]){G.find("em").html(C);}else{G.html(C);}
								G.find("span").show();
								$(this.idPrefix+"addToCart,"+this.idPrefix+"addmake,"+this.idPrefix+"nowbuyToCart,"+this.idPrefix+"goods_pointsexchange_info").show();
								$(this.idPrefix+"noAddToCart").hide();
							}
						}else{
							G.find("em").html(lodo_lang_js.goods_spec.maxqn);
							G.find("span").hide();
						}
					//}
					var M=$(this.idPrefix+"buy_maxCount");
					if(M[0]){$(M).val(C);}
					var O=$(this.idPrefix+"point");
					var I=$(this.idPrefix+"point_default");
					if(!selected){P=O.attr("point")}
					if(O[0]){
						O.show().html(P);
/*						if(P>0){
							//O.html(P).show();
							//I.hide();
						}else{
							//O.hide();
							//I.show();
						}*/
					}
					var U=$(this.idPrefix+"price1");
					var K=$(this.idPrefix+"price2");
					if(U[0]&&K[0]){
						if(!isfloat(Q)||Q=="0.00"){
							U.hide();
							K.show();
							Q=K.attr("gprice");
						}else{
							U.find("em").html(Q);
							U.show();
							K.hide();
						}
					}
					var goodsdynamic=$(this.idPrefix+"goodsdynamic");
					if(goodsdynamic[0]){
						if(!isfloat(Q)){
							$(this.idPrefix+"goodsdynamic>span.totalinfo>i.g-total-price").parent().html("<i class='g-total-price'>"+Q+"</i>");
						}else{
							goodsdynamic.attr({gprice:Q,gpoints:P});
							$(this.idPrefix+"goodsdynamic>span.totalinfo>i.g-total-price").html(Q);
							$(this.idPrefix+"goodsdynamic>span.totalPoint>i.g-total-points").html(P);
						}
					}
					var N=$(this.idPrefix+"priceSave");
					var Z=$(this.idPrefix+"goodsdiscount");
					if(N[0]){
						if(!isfloat(T)||T=="0.00"){
							var fold=Z.attr("fold");
							if(!(isfloat(fold))){
								Z.parent().hide();
							}else{
								Z.html(fold);
								Z.parent().show();
							}
							var dft=N.attr("dft");
							if(!(isfloat(dft))){
								N.parent().hide();
							}else{
								N.html(dft);
								N.parent().show();
							}
						}else{
							if(Z[0]&&D!=""){Z.html(D);}
							N.html(T);
							N.parent().show();
						}
					}
				}else{
					var V=$(this.idPrefix+"otherItem_"+L);
					var E=$(this.idPrefix+"otherItem_href_"+L);
					if(V[0]&&E[0]){
						if(this.selectCPID.length==this.sellPropsCount&&C==0){$(V).hide();}else{$(V).show();}
						if(E.attr("href").indexOf("?")!=-1){$(E).attr("href",$(E).attr("href").substring(0,$(E).attr("href").indexOf("?")));}
						if(F!=0){$(E).attr("href",$(E).attr("href")+"?itemDetailId="+F);}
					}
				}
				
				
			}
		},
		getSelectedItem:function(){
			var J=this.getCurrentPV();
			for(var S=0;S<this.allItems.length;S++){
			var B=this.allItems[S].itemDetils;
			var F=null;
			for(var R=0;R<B.length;R++){
				var A=B[R];
				if(A.prop==J){
					F=A;
					break
				}
			}
			return F;
			}
		},
		getCurrentPV:function(){
			this.selectCPID.sort(function(D,C){return C-D});
			var A=[];
			for(var B=0;B<this.selectCPID.length;B++)
			{
				A.push(this.selectPVID[this.selectCPID[B]])
			}
			if(A.length==0){return ""}else{return ";"+A.join(";")+";"}
		},
		getUnselectedPVNames:function(){
			var B=[];
			for(var A in this.sellProps){
				if((""+this.selectCPID+"").indexOf(""+A+"")==-1){
					B.push(this.sellProps[A].name)
				}
			}
			return B
		},
		getSelectedValueNames:function(){
			var B=[];
			for(var A in this.sellProps){
				if((""+this.selectCPID+"").indexOf(""+A+"")!=-1){
					var C=this.selectPVID[A];
					B.push(this.sellProps[A].pvs[C].name)
				}
			}
			return B
		},
		getAllPVNames:function(){
			var B=[];
			for(var A in this.sellProps){
				B.push(this.sellProps[A].name)
			}
			return B
		},
		isAllPVSelected:function(){return this.selectCPID.length==this.sellPropsCount},
		isNoPVSelected:function(){return this.selectCPID.length==0}
	}
})();